﻿
//CALLBACK == passing a function as an argument
// the argument being passed in, is the CALLBACK
function myMessage(peanutbutter, nameToBeAdded) {
    //console.log(str);
    if (typeof peanutbutter == "function") {
        console.log("argument is a function");
        var mydata = "hola senor";
        peanutbutter(mydata);
    } else if (typeof peanutbutter == "object") {
        peanutbutter.push(nameToBeAdded);
        for (var i in peanutbutter) {
            console.log(peanutbutter[i])
        }
        
    } else {
        console.log("argument is NOT a function nor an object");
    }
}

// alert a message
function greet() {
    alert("howdy ya'll!");
}
// alert the argument
function showData(jelly) {
    //console.log(data);
    alert(jelly);
}
//showData("Bonjour");


//myMessage(greet);
//myMessage(showData);


// array of names
var users = ["sue", "susan", "will", "smith", "tyson"];
//myMessage(users, "Scarlet");

// ajax call w/ callback
function fetchFbData(jelly) {
    var request = new XMLHttpRequest();
    request.open('GET', 'https://contact413.firebaseio.com/Contacts/.json', true);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            //Do Something On Success
            if (typeof jelly == "function") {
                var convertedData = JSON.parse(this.response);
                jelly(convertedData);
            }
        }
        else {
            //Do Something On Error
        }
    };
    request.send();
}
// callback to be used in ajaxcall
function showResults(data) {
    for (var i in data) {
        console.log(data[i].fName);
    }
}
//fetchFbData(showResults);

function getFBInterval() {
    fetchFbData(showResults);
}


////// Polling /////
/*
var myTimeout = setTimeout(function () {
    console.log("pizza");
    clearInterval(myTimer);
}, 4000);
*/

var sayHey = function () {
    console.log("hey!");
}
// setInterval
//setInterval(getFBInterval, 3000);
/*
setInterval(function () {
   console.log("lunch break!");
},3000);
*/
//var myTimer = setInterval(getFBInterval, 1000)

// function fired when user 'click'
var userClicked = function (evenzt) {
    console.log("userClicked: ", evenzt);
}
// get element with ID of 'AddContact', and assign to a var 
var btn = document.getElementById("AddContact");
// bind the func to the 'click' event
btn.addEventListener('mouseover', userClicked);
//btn.addEventListener('mouseout', userClicked);

// Jquery equivalent of addEventListener (bind);
$("#UpdateContact").bind("click", userClicked);

///var el = document.getElementById("outside");
//el.addEventListener("click", modifyText, false);


var objToLoop = { "city1": "Pearland", "city2": "Miami" };


var objToLoop_other = [
    { "city1": "Pearland", "city2": "Miami" },
    { "city1": "Houston", "city2": "Dallas" }
];

var objToLoop_third = [
    {
        "city1": { "name": "Pearland", "loc": "21551, 65465546" },
        "city2": { "name": "Miami", "loc": "345345, 322" }
    },
    {
        "city1": { "name": "Hoston", "loc": "1111, 2222" },
        "city2": { "name": "Dallas", "loc": "333, 44" }
    }
];
//objToLoop_third[0].name


for (var i in objToLoop_third) {
    var step = objToLoop_third[i];
    //console.log("LevelOne: ", step);
    for (var ii in step) {
        //console.log("    LevelTwo: ", step[ii]);
        var innerStep = step[ii];
        for (var iii in innerStep) {
            console.log("    LevelThree: ", innerStep[iii]);
        }
    }
}
